//
//  AddContactVC.m
//  CJRailTransport
//
//  Created by Hank on 2018/2/24.
//  Copyright © 2018年 amplity. All rights reserved.
//

#import "AddContactVC.h"
#import "AddContactCell.h"

@interface AddContactVC ()

@end

@implementation AddContactVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置导航栏
    [self setNavC];
    // 设置界面属性
    [self setViewAttribute];
    // 注册Cell
    [self registerCells];
    // 设置数据
    [self setData];
}

#pragma mark - 导航栏
- (void)setNavC
{
    self.title = @"请选择联系人";
    // 设置导航栏颜色
    BasicNavigationController *nVC = (BasicNavigationController *)self.navigationController;
    nVC.themeColor = [UIColor whiteColor];
    nVC.tintColor = [UIColor darkGrayColor];
}

// 设置界面属性
- (void)setViewAttribute
{
    self.view.backgroundColor = [UIColor whiteColor];
}

#pragma mark - initArray
// 初始化数组
- (NSMutableArray *)allDataArray
{
    if (!_allDataArray) {
        _allDataArray = [[NSMutableArray alloc]init];
    }
    return _allDataArray;
}

- (NSMutableArray *)selectDataArray
{
    if (!_selectDataArray) {
        _selectDataArray = [[NSMutableArray alloc]init];
    }
    return _selectDataArray;
}

// 注册Cell
- (void)registerCells
{
    UINib *nib = [UINib nibWithNibName:[AddContactCell reuseIdentifier] bundle:nil];
    [_myTableView registerNib:nib forCellReuseIdentifier:[AddContactCell reuseIdentifier]];
}

// 设置数据
- (void)setData
{
    // 测试数据
    for (int i = 0; i < 5; i++) {
        AddContactModel *test = [[AddContactModel alloc]init];
        test.name = [NSString stringWithFormat:@"张三%d",i];
        test.email = [NSString stringWithFormat:@"32131231%d@qq.com",i];
        test.image = @"login_userName";
        // 判断是否替换
        BOOL judgeReplace = NO;
        // 遍历选择过的联系人并替换地址
        for (int i = 0; i < self.selectDataArray.count; i++) {
            AddContactModel *addContactModel = self.selectDataArray[i];
            if ([addContactModel.name isEqualToString:test.name] && [addContactModel.email isEqualToString:test.email] && [addContactModel.image isEqualToString:test.image]) {
                [self.allDataArray addObject:addContactModel];
                judgeReplace = YES;
                break;
            }
        }
        // 如未选择过联系人则添加联系人
        if (!judgeReplace) {
            [self.allDataArray addObject:test];
        }
    }
}

#pragma mark - TableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 防止循环引用
    WEAK_REF(self);
    
    AddContactCell *cell = [tableView dequeueReusableCellWithIdentifier:[AddContactCell reuseIdentifier] forIndexPath:indexPath];
    cell.indexPath = indexPath;
    [cell setData:self.allDataArray selectData:self.selectDataArray];
    // 复选框点击 Block
    [cell setCheckImageClickBlock:^(NSIndexPath *indexPath, AddContactModel *content, BOOL state){
        //======修改Model 自定义字段======
        if (state) {
            [weak_self.selectDataArray addObject:content];
        }else{
            // 移除指定元素
            [weak_self.selectDataArray removeObject:content];
        }
        // 刷新单个Cell
        [weak_self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
        // 回调数据
        weak_self.checkImageClickBlock(content, state, weak_self.stateTag);
    }];
    return cell;
}
#pragma mark - TableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
